<?php
    require_once('popup_base_jectly.php');

	/*
     * sub classes of page_base_jectly will include only the changes from index_2;
	 */
	class popup_accept_cancel extends popup_base_jectly {

        protected $contents_html='<div class=\'center\'>This is popup_accept_cancel page.</div>';

        protected $id;//project_id
        public $project;
        protected $uid;//target user_id
        public $target_user;
             public $bool_confirming=false;
        public $bool_message_exist=false;
        public $bool_error=false;
        public $bool_permitted=false;
		public $button_confirm;
        public $button_cancel;
        public $request_id;
        public $request;
        public $str_permission_error_text;
         
        public $str_confirming_text='<br/><br/>Please confirm to <strong>accept</strong> canceling request.<br /><br />';
        
/*        public $str_confirming_text_user="<br/><br/You have to <strong>communicate </strong>with the buyer.<br /><br />";*/
        
/*         public $str_confirming_text_user1='<br/><br/>If your accept this request, this project will be <strong>failed </strong>and you can not communicate with the buer no more throught this project.<br /><br />';*/

        function __construct() {
            parent::__construct();
        }

        protected function Form_Create() {

            $this->check_query_string();

            $this->load_project();
            $this->load_target_user();
              	if($this->bool_popup_accept_cancel_permitted())
            {
                $this->bool_permitted=true;
            }
			
			
			  if($this->bool_permitted)
            {
                
                $this->button_confirm_Create();
                $this->button_cancel_Create();
                
				}
		}
		 protected function button_confirm_Create() {
            $this->button_confirm = new QButton($this);
            $this->button_confirm->Text = 'confirm';
            $this->button_confirm->AddAction(new QClickEvent(), new QServerAction('confirm_Click'));
            $this->button_confirm->PrimaryButton = true;
        }
	   protected function button_cancel_Create() {
            $this->button_cancel = new QButton($this);
            $this->button_cancel->Text = 'cancel';
            $this->button_cancel->AddAction(new QClickEvent(), new QServerAction('cancel_Click'));
            //$this->button_cancel->PrimaryButton = true;
        }
		   public function confirm_Click($strFormId, $strControlId, $strParameter) {
		   	
		   	$objUser = unserialize($_SESSION['User']);
            $this->UserId=$objUser->Id;
        	
            $request=TableProjectRequestCancel::LoadById($this->request_id);
       	    $request->set_status_retracted();
			$request->Save(false,true);

            QApplication::ExecuteJavaScript('opener.location.reload(true);self.window.close();');
        }
         public function cancel_Click($strFormId, $strControlId, $strParameter) {
            QApplication::ExecuteJavaScript('self.window.close();');
        }
       
        
        protected function check_query_string()
        {
            if(is_numeric(QApplication::QueryString('id')))
            {
                $this->id=QApplication::QueryString('id');//project_id
            }

            if(is_numeric(QApplication::QueryString('uid')))
            {
                $this->uid=QApplication::QueryString('uid');//provider_id
            }
        }

        protected function load_project()
        {
            if(isset($this->id) && is_numeric($this->id))
            {
                $this->project=TableProject::LoadById($this->id);
                  	$this->request_id=TableProjectRequestCancel::load_request_id_for_project($this->id);
            	$this->request=TableProjectRequestCancel::LoadById($this->request_id);
            	$this->uid=TableProjectWon::Won_Provider_id_after_starting($this->id);
            	                if(isset($this->project))
                {
                    $this->contents_html='project loaded-['.$this->project->Title.'] user_id=['.$this->project->UserId.']';
                }
                else
                {
                    $this->contents_html='project not found id=['.$this->id.']';
                    $this->id=null;
                    $this->project=null;
                }
            }
        }

        protected function load_target_user()
        {
            if(isset($this->uid) && is_numeric($this->uid))
            {
                $this->target_user=TableUserProject::LoadById($this->uid);

                if(isset($this->target_user))
                {
                    $this->contents_html.=' target_user loaded-['.$this->target_user->UserName.'] user_id=['.$this->target_user->Id.']';
                }
                else
                {
                    $this->contents_html.=' target_user not found id=['.$this->uid.']';
                    $this->uid=null;
                    $this->target_user=null;
                }
            }
        }

        //check permissions
        public function bool_project_loaded()
        {
            return isset($this->project);
        }
            public function bool_request_loaded()
        {
            return isset($this->request);
        }


        public function bool_target_user_loaded()
        {
            return isset($this->target_user);
        }

        public function bool_my_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_my_project($this->UserId);
            }

            return false;
        }

        public function bool_won_project()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_won_project($this->UserId);
            }

            return false;
        }

        public function bool_already_bidding()
        {
            return false;
        }

        public function bool_can_bid()
        {
            return false;
        }

        public function bool_project_open()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_open_project();
            }

            return false;
        }

        public function bool_project_after_picked()
        {
            if(isset($this->project) && isset($this->UserId))
            {
                return $this->project->bool_after_picked_project();
            }

            return false;
        }

        public function bool_project_bidding_end()
        {
            if(!$this->bool_project_open() && !$this->bool_project_after_picked())return true;

            return false;
        }
     
        protected function bool_popup_accept_cancel_permitted()
        {
           
            if(!$this->bool_logined()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('You must login to access this page at least');
                return false;
            }
                if(!$this->bool_request_loaded()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('there is no request to retract the project');
                return false;
            }
            if(!$this->bool_project_loaded()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('Project not found');
                return false;
            }
            if($this->bool_my_project()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project is yours');
                return false;
            }
           
            if(!$this->bool_project_open()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project is closed for bidding');
                return false;
            }
              if(!$this->bool_won_project()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The project has has not been started');
                return false;
            }
           if($this->  bool_project_request_retract_cancel()){
                $this->reset_permission_error_text();
                $this->append_line_permission_error_text('The cancel request for retract the  project has been made already been accepted');
                return false;
            }

            //personal informaion input & user level & bidding count etc
            //will be checked for bidding after my_account_settting page is done.

            return true;
        }
        protected function bool_status()
	{
		if(isset($this->project)&& isset($this->UserId) && isset($this->request))
		{
			return $this->request->bool_created();
		}
		return false;

	}

       public function render_permission_error_text() {

            if(!$this->bool_permitted)
            {
                echo($this->str_permission_error_text);
            }
        }
        
        public function render_message_text() {

            if($this->bool_message_exist)
            {
                echo($this->str_message);
            }
        }

        public function render_confirming_text() {

            if($this->bool_confirming)
            {
                echo($this->str_confirming_text);
            }
        }

        public function set_error_message($str_) {

            if(!$this->bool_error)
            {
                $this->reset_message();
                $this->append_line_message($str_);
                $this->bool_error=true;
            }
        }

        public function reset_error_message() {
            $this->reset_message();
            $this->bool_error=false;
            $this->bool_message_exist=false;
        }

        public function append_permission_error_text($str_) {

            $this->str_permission_error_text.=$str_;
        }

        public function append_line_permission_error_text($str_) {

            $this->append_permission_error_text($str_.'<br />');
        }

        public function reset_permission_error_text() {

            $this->str_permission_error_text='';
        }

        public function append_message($str_) {

            $this->str_message.=$str_;
        }

        public function append_line_message($str_) {

            $this->append_message($str_.'<br />');
        }

        public function reset_message() {

            $this->str_message='';
        }

        public function append_confirming_text($str_) {

            $this->str_confirming_text.=$str_;
        }

        public function append_line_confirming_text($str_) {

            $this->append_confirming_text($str_.'<br />');
        }

        public function reset_confirming_text() {

            $this->str_confirming_text='';
        }

        public function render_contents_area() {

            if($this->bool_contents_area_on)
            {
                echo($this->contents_html);
            }
        }
          public function bool_project_request_retract_cancel()
        {
            if(isset($this->request) && isset($this->UserId))
            {
                if($this->request->count_bid_for_retract_cancel()>0)
                {
                    return true;
                }
            }

            return false;
        }
        
      


	}

    popup_accept_cancel::Run('popup_accept_cancel','popup_accept_cancel.tpl.php');

?>